<template>
  <div class="app-container">
    <el-form label-width="120px">
      <el-form-item label="教师姓名">
        <el-input v-model="Teacher.name" placeholder="请输入教师姓名" />
      </el-form-item>
      <!-- 讲师头像：TODO -->
      <el-form-item label="头像">
        <!-- 头衔缩略图 -->
        <pan-thumb :image="Teacher.pic" />
        <!-- 文件上传按钮 -->
        <el-button
          type="primary"
          icon="el-icon-upload"
          @click="imagecropperShow = true"
          >上传图片
        </el-button>
        <!--v-show：是否显示上传组件
:key：类似于id，如果一个页面多个图片上传控件，可以做区分
:url：后台上传的url地址
@close：关闭上传组件
@crop-upload-success：上传成功后的回调 -->
        <image-cropper
          v-show="imagecropperShow"
          :width="70"
          :height="70"
          :key="imagecropperKey"
          :url="BASE_API + '/oss/fileUpload'"
          field="file"
          @close="close"
          @crop-upload-success="cropSuccess"
        /><br/>
        <br/>


      <el-select v-model="Teacher.position" placeholder="请选择教师职位">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>


      </el-form-item>
      <el-form-item label="简介">
        <el-input v-model="Teacher.decreption" placeholder="请输入个人简介" type="textarea" />
      </el-form-item>
      <el-form-item label="排序号">
        <el-input-number
          v-model="Teacher.sortId"
          controls-position="right"
          :min="0"
        />请填写排序号,数字越小越靠前
      </el-form-item>
      <el-form-item>
        <el-button
          :disabled="saveBtnDisabled"
          type="primary"
          @click="saveOrUpdate"
          >保存</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import TeacherAPI from "@/api/Teacher";
import ImageCropper from "@/components/ImageCropper";
import PanThumb from "@/components/PanThumb";

export default {
  components: { PanThumb, ImageCropper },
  data() {
      //定义变量和初始值
    return {
      Teacher: {},
      saveBtnDisabled: false, // 保存按钮是否禁用,
      imagecropperShow: false, //头像上传组件是否显示
      imagecropperKey: 0,
      BASE_API: process.env.BASE_API, //获取dev.env.js里面的地址
      options: [{
          value: '语文老师',
          label: '语文老师'
        }, {
          value: '数学老师',
          label: '数学老师'
        }, {
          value: '英语老师',
          label: '英语老师'
        }, {
          value: '化学老师',
          label: '化学老师'
        }, {
          value: '生物老师',
          label: '生物老师'
        }],
        position: '',
    };
  },
  created() {
    //页面渲染之前执行
    this.init();
  },
  watch: {
    //监听
    $route(to, from) {
      //路由变化方式，路由发生变化，方法就会执行
      this.init();
    },
  },
  methods: {
    close() {
      //关闭上传头像组件
      this.imagecropperShow = false;
      //上传失败后，重新打开上传组件时初始化组件，否则显示上一次的上传结果
      this.imagecropperKey = this.imagecropperKey + 1;
    },
    cropSuccess(data) {
      //上传成功后返回的图片地址
      this.Teacher.pic = data.fileUrl;
      //上传头像成功
      this.imagecropperShow = false;
      //上传成功后，重新打开上传组件时初始化组件，否则显示上一次的上传结果
      this.imagecropperKey = this.imagecropperKey + 1;
    },
    init() {
      //判断路径有id值,做修改
      if (this.$route.params && this.$route.params.id) {
        //从路径获取id值
        const id = this.$route.params.id;
        //调用根据id查询的方法
        this.getInfo(id);
      } else {
        //路径没有id值，做添加
        //清空表单
        this.Teacher = {};
      }
    },
    //根据讲师id查询的方法
    getInfo(id) {
      TeacherAPI.selectOne(id).then((response) => {
        this.Teacher = response.object;
      });
    },
    saveOrUpdate() {
      this.Teacher.emplId=1
      //判断修改还是添加
      //根据teacher是否有id
      if (!this.Teacher.id) {
        //添加
        this.addTeacher();
      } else {
        //修改
        this.editTeacher();
      }
    },
    //修改教师信息的方法
    editTeacher() {
      TeacherAPI.updayteOne(this.Teacher).then((response) => {
        //修改成功
        //提示信息
        this.$message({
          type: "success",
          message: "修改成功!",
        });
        //回到教师管理页面 路由跳转
        this.$router.push({ path: "/list/starTeacher" });
      });
    },
    //添加教师的方法
    addTeacher() {
      TeacherAPI.insertOne(this.Teacher).then((response) => {
        //添加成功
        //提示信息
        this.$message({
          type: "success",
          message: "添加成功!",
        });
        //回到教师管理页面 路由跳转
        this.$router.push({ path: "/list/starTeacher" });
      });
    },
  },
};
</script>
<style>
  .el-dropdown-link {
    cursor: pointer;
    color: #409EFF;
  }
  .el-icon-arrow-down {
    font-size: 12px;
  }
</style>
